<template>
	<view class="class-list">
		<view class="content">
			<view class="item" v-for="i in classInfoList" :key="i._id">
				<view  @click="goToPreview(i._id)">
					<image :src="i.smallPicurl" mode="aspectFill"></image>
				</view>
			</view>
		</view>
	</view>
	<uni-load-more status="loading" v-if="!nonClass"></uni-load-more>
	<uni-load-more status="no-more" v-if="nonClass"></uni-load-more>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {onLoad,onReachBottom} from "@dcloudio/uni-app"
import {request} from "../../utils/request.js"
let classid = ref('')
let classInfoList = ref([])
let pageNum = ref(1)
let nonClass = ref(false)
let type = ref("")
let keyword = ref("")
onMounted(()=>{
	
})
onLoad((e)=>{
	classid.value = e.classid
	type.value = e.type
	keyword.value = e.keyword
	console.log(e);
	if(classid.value){
		getWallList()
	}else if(type.value == "search"){
		uni.setNavigationBarTitle({
			title:"搜索"
		})
		getSearchList()
	}else{
		if(type.value == "score"){
			uni.setNavigationBarTitle({
				title:"我的评分"
			})
		}else{
			uni.setNavigationBarTitle({
				title:"我的下载"
			})
		}
		getScoreDownload()
	}
	
})
onReachBottom(()=>{
	if(nonClass.value == true){
		return
	}
	if(classid.value){
		getWallList()
	}else if(type.value == "search"){
		getSearchList()
	}else {
		getScoreDownload()
	}
})
//获取壁纸列表
const getWallList = () =>{
	request({
		url:"/wallList",
		data:{
			classid:classid.value,
			pageNum:pageNum.value
		}
	}).then(res=>{
		classInfoList.value = classInfoList.value.concat(res.data.data)
		uni.setStorageSync("classList",classInfoList.value)
		pageNum.value++
		if(res.data.data.length < 12){
			console.log(res.data.data.length);
			nonClass.value = true
		}
		console.log(pageNum.value);
	})
	// uniCloud.callFunction({
	// 	name:"getWallList",
	// 	data:{
	// 		classid:classid.value,
	// 		pageNum:pageNum.value
	// 	}
	// }).then(res=>{
	// 	classInfoList.value = classInfoList.value.concat(res.result.data)
	// 	uni.setStorageSync("classList",classInfoList.value)
	// 	pageNum.value++
	// 	if(res.result.data.length ==0){
	// 	nonClass.value = true
	// 	}
	// })
}
//获取评分下载列表
const getScoreDownload = () =>{
	uniCloud.callFunction({
		name:"getScoreDownload",
		data:{
			type:type.value,
			pageNum:pageNum.value
		}
	}).then(res=>{
		classInfoList.value = classInfoList.value.concat(res.result.data)
		uni.setStorageSync("classList",classInfoList.value)
		pageNum.value++
		if(res.result.data.length ==0){
		nonClass.value = true
		}
	})
}
//前往壁纸详情页
const goToPreview = (id) =>{
	uni.navigateTo({
		url:"/pages/preview/preview?id="+id,
	})
}
//获取搜索列表
const getSearchList = () =>{
	request({
		url:"/searchWall",
		data:{
			keyword:keyword.value,
			pageNum:pageNum.value
		}
	}).then(res=>{
		uni.setStorageSync("classInfoList",classInfoList.value.concat(res.data.data))
		classInfoList.value = uni.getStorageSync("classInfoList") || []
		pageNum.value++
		if(res.data.data.length <12){
			nonClass.value = true
		}
	})
}
</script>

<style lang="scss">
.class-list{
	.content{
		display: grid;
		grid-template-columns: repeat(3,1fr);
		gap: 5rpx;
		padding: 5rpx;
		.item{
			image{
				width: 100%;
				height: 440rpx;
				display: block;
			}
		}
	}
}
</style>
